﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.LinqBuilder;
using Queer.Entity;
using Queer.IService.SystemManage;
using Queer.Model.Param.SystemManage;
using Queer.Util;
using Queer.Util.Helper;
using Queer.Util.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Queer.Service.SystemManage
{
    /// <summary>
    /// 创 建：
    /// 日 期：2020-12-04 12:49
    /// 描 述：Api日志服务类
    /// </summary>

    public class LogApiService : ILogApiService, ITransient
    {
        private readonly ISqlSugarRepository<LogApiEntity> _logApiDB;

        public LogApiService(ISqlSugarRepository<LogApiEntity> logApiDB)
        {
            _logApiDB = logApiDB;
        }

        #region 获取数据

        /// <summary>
        /// 带条件查询所有
        /// </summary>
        public async Task<List<LogApiEntity>> GetList(LogApiListParam param)
        {
            var data = await ListFilter(param).ToListAsync();
            return data;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        public async Task<List<LogApiEntity>> GetPageList(LogApiListParam param, Pagination pagination)
        {
            ISugarQueryable<LogApiEntity> query = ListFilter(param);

            var data = await query.OrderBy(a => a.Id, OrderByType.Desc).ToPagedListAsync(pagination.PageIndex, pagination.PageSize);

            // 分页参数赋值
            pagination.TotalCount = data.TotalCount;
            return data.Items.ToList();
        }

        /// <summary>
        /// 根据ID获取对象
        /// </summary>
        public async Task<LogApiEntity> GetEntity(long id)
        {
            var list = await _logApiDB.AsQueryable(p => p.Id == id).ToListAsync();
            return list.FirstOrDefault();
        }

        /// <summary>
        /// 查询多个ID主键数据
        /// </summary>
        public async Task<List<LogApiEntity>> GetListByIds(string ids)
        {
            if (ids.IsNullOrEmpty())
                throw new BusinessException("参数不合法！");

            var idArr = TextHelper.SplitToArray<long>(ids, ',').ToList();
            var data = await _logApiDB.AsQueryable(a => idArr.Contains(a.Id.GetValueOrDefault())).ToListAsync();

            return data;
        }

        #endregion

        #region 提交数据

        public async Task SaveForm(LogApiEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                // 默认赋值
                entity.Id = IdGeneratorHelper.Instance.GetId();
                await _logApiDB.InsertAsync(entity);
            }
            else
                await _logApiDB.UpdateAsync(entity);
        }

        public async Task DeleteForm(string ids)
        {
            if (string.IsNullOrWhiteSpace(ids))
                throw new BusinessException("参数不合法！");

            var _ids = ids.Split(",").Select(a => a.ParseToLong());
            await _logApiDB.Context.Deleteable<LogLoginEntity>().In(_ids).ExecuteCommandAsync();
        }

        #endregion

        #region 私有方法

        private ISugarQueryable<LogApiEntity> ListFilter(LogApiListParam param)
        {
            ISugarQueryable<LogApiEntity> query = _logApiDB.AsQueryable();

            if (param.LogStatus > -1)
                query = query.Where(p => p.LogStatus == param.LogStatus);

            if (!string.IsNullOrEmpty(param.IpAddress))
                query = query.Where(p => p.IpAddress.Contains(param.IpAddress));

            if (param.StartTime.HasValue)
                query = query.Where(p => p.LogTime >= param.StartTime);

            if (param.EndTime.HasValue)
                query = query.Where(p => p.LogTime <= param.EndTime.Value.AddDays(1));

            if (!string.IsNullOrEmpty(param.UserName))
                query = query.Where(p => p.LogUser.Contains(param.UserName));

            return query;
        }
        #endregion
    }
}
